package com.fastloan.mgt.invest.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.invest.dao.InvestTempVarMapper;
import com.fastloan.mgt.invest.model.InvestTemp;
import com.fastloan.mgt.invest.model.InvestTempVar;
import com.fastloan.mgt.invest.model.InvestType;
import com.fastloan.mgt.invest.model.vo.InvestTempVarVo;
import com.fastloan.mgt.invest.service.InvestTempService;
import com.fastloan.mgt.invest.service.InvestTempVarService;
import com.fastloan.mgt.invest.service.InvestTypeService;
import com.fastloan.safe.mgt.org.model.CmuOrg;
import com.fastloan.safe.mgt.org.model.CmuOrgUser;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.org.service.CmuOrgUserService;
import com.fastloan.safe.mgt.sys.service.CmSysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:34
 */
@Service
public class InvestTempVarServiceImpl extends BasicServiceImpl<InvestTempVar> implements InvestTempVarService {

    @Autowired
    private InvestTempVarMapper investTempVarMapper;

    @Autowired
    private InvestTempService investTempService;

    @Autowired
    private InvestTypeService investTypeService;

    @Autowired
    private CmuOrgUserService orgUserService;

    @Autowired
    private CmuOrgService orgService;

    @Autowired
    private CmSysFileService cmSysFileService;

    @Override
    @Transactional
    public void insertSort(String tempId, String fromIds) {
        String[] split = fromIds.split(",");
        List<InvestTempVar> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            InvestTempVar investTempVar = new InvestTempVar();
            investTempVar.setTempId(tempId);
            investTempVar.setFromId(split[i]);
            investTempVar.setShorts(i);
            list.add(investTempVar);
        }
        investTempVarMapper.insertList(list);
    }

    @Override
    @Transactional
    public void updateSort(InvestTempVar investTempVar) {
        InvestTempVar investTempVarOld = selectByPrimaryKey(investTempVar.getId());
        if (investTempVar.getShorts() != null && !investTempVarOld.getShorts().equals(investTempVar.getShorts())) {
            updateSortBatch(investTempVar.getShorts(), investTempVarOld.getTempId(), investTempVarOld.getShorts(), false);
        }
        updateByPrimaryKeySelective(investTempVar);
    }

    @Override
    @Transactional
    public void deleteSort(Integer investTempVarId) {
        InvestTempVar investTempVar = selectByPrimaryKey(investTempVarId);
        updateSortBatch(investTempVar.getShorts(), investTempVar.getTempId(), null, true);
        deleteByPrimaryKey(investTempVarId);
    }

    @Override
    public List<InvestTempVarVo> selectByTempId(String tempId, String typeId) {
        return investTempVarMapper.selectByTempId(tempId, typeId);
    }

    @Override
    @Transactional
    public String insertSortOne(InvestTempVar investTempVar) {
        updateSortBatch(investTempVar.getShorts(), investTempVar.getTempId(), null, false);
        insertSelective(investTempVar);
        return investTempVar.getId();
    }

    @Override
    public Map<String, Object> selectTempByUrl(String tempId, String managerId, String typeId) {
        Map<String, Object> map = new HashMap<>();
        InvestTemp investTemp = investTempService.selectByPrimaryKey(tempId);
        map.put("content", investTemp.getContent());
        if (typeId == null) {
            List<InvestType> investTypeList = null;
            String typeIds = investTemp.getTypeIds();
            if (!"0".equals(typeIds)) {
                String[] split = investTemp.getTypeIds().split(",");
                List<String> list = Arrays.asList(split);
                Example example = new Example(InvestType.class);
                example.createCriteria().andIn("id", list);
                investTypeList = investTypeService.selectByExample(example);
            }
            CmuOrgUser cmuOrgUser = new CmuOrgUser();
            cmuOrgUser.setUserId(managerId);
            cmuOrgUser = orgUserService.selectOne(cmuOrgUser);
            CmuOrg cmuOrg = orgService.selectByPrimaryKey(cmuOrgUser.getOrgId());
            if (cmuOrg != null && cmuOrg.getWeixinCode() != null) {
                map.put("orgImg", cmSysFileService.selectByPrimaryKey(cmuOrg.getProfile()).getUrl());
                map.put("orgName", cmuOrg.getName());
            }
            map.put("investTypeList", investTypeList);
            map.put("title", investTemp.getTitle());
        } else {
            List<InvestTempVarVo> investTempVarList = investTempVarMapper.selectByTempId(tempId, typeId);
            map.put("investTempVarList", investTempVarList);
            map.put("haveBase", investTempVarList.size() > 0 ? investTempVarList.get(0).getHaveBase() : 0);
            map.put("title", investTempVarList.size() > 0 ? investTempVarList.get(0).getTittle() : null);
        }
        map.put("managerId", managerId);
        return map;
    }

    private void updateSortBatch(Integer shorts, String tempId, Integer oldSorts, boolean isDel) {
        Example example = new Example(InvestTempVar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tempId", tempId);
        int impairment = 1;
        if (oldSorts == null) {
            if (isDel) {
                impairment = -1;
            }
            criteria.andGreaterThanOrEqualTo("shorts", shorts);
        } else {
            if (shorts > oldSorts) {
                criteria.andBetween("shorts", oldSorts + 1, shorts);
                impairment = -1;
            } else {
                criteria.andBetween("shorts", shorts, oldSorts - 1);
            }
        }
        List<InvestTempVar> investTempVars = selectByExample(example);
        if (investTempVars.size() != 0) {
            for (InvestTempVar tempVar : investTempVars) {
                tempVar.setShorts(tempVar.getShorts() + impairment);
                updateByPrimaryKeySelective(tempVar);
            }
        }
    }
}
